package com.amc.dao.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.amc.dao.IUserDao;
import com.amc.entity.Address;
import com.amc.entity.Customer;

@Repository
public class UserDaoImpl implements IUserDao {

	@PersistenceContext
	private EntityManager em;

	public void setEm(EntityManager em) {
		this.em = em;
	}

	@Override
	public boolean insertUser(String username, String password) {
		Customer customer = new Customer();
		customer.setCustomerName(username);
		customer.setCustomerPassword(password);
		Set<Address> set = new HashSet<>();
		set.add(new Address(null, "必填", "必填", "必填"));
		customer.setAddressSet(set);
		boolean result = false;
		try {
			em.persist(customer);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("User 插入错误");
		}
		return result;
	}

	@Override
	public Customer queryUser(String username, String password) {
		Customer customer = null;
		Query query = em
				.createQuery(
						"select c from Customer c join fetch c.addressSet where c.customerName=? and c.customerPassword=? ")
				.setParameter(1, username).setParameter(2, password);
		try {
			customer = (Customer) query.getSingleResult();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("用户账户密码查询错误");
		}
		return customer;
	}

	@Override
	public boolean insertAddress(Address address, String customerId) {
		boolean result=false;
		try {
			Customer customer = em.find(Customer.class, customerId);
			Set<Address> addresses = customer.getAddressSet();
			addresses.add(address);
			em.persist(address);
			result=true;
		} catch (Exception e) {
			System.out.println("用户地址插入失败");
		}
		return result;
	}

	@Override
	public boolean updateAddress(Address address,String customerId) {//要发customerId 和Address过来
		boolean result = false;
		try {
			Customer customer=em.find(Customer.class, customerId);
			Address adr = em.find(Address.class, address.getAddressId());
			Set<Address> ads=customer.getAddressSet();
			for(Address adss:ads){
				if(adss.getAddressId().equals(adr.getAddressId())){
					adss.setAddressContactPerson(address.getAddressContactPerson());
					adss.setAddressContactTel(address.getAddressContactTel());
					adss.setAddressName(address.getAddressName());
//					em.remove(adr);
//					em.persist(adss);
					break;
				}
			}			
			result = true;
		} catch (Exception e) {
			System.out.println("用户地址更新失败");
		}
		return result;
	}

	@Override
	public boolean deleteAddress(String addressId,String customerId) {
		boolean result = false;
		Customer customer=em.find(Customer.class, customerId);
		try {
			Address adr = em.find(Address.class, addressId);
			Set<Address> ads=customer.getAddressSet();
			for(Address adss:ads){
				if(adss.getAddressId().equals(adr.getAddressId())){
					ads.remove(adss);
					em.remove(adr);
					break;
				}
			}
//			em.remove(adr);
			result = true;
		} catch (Exception e) {
			System.out.println("用户地址更新失败");
		}
		return result;
	}

	@Override
	public List<Address> queryAllAddress(String customerId) {
		List<Address> address = null;
		try {
			Customer customer = em.find(Customer.class, customerId);
			address = new ArrayList<Address>(customer.getAddressSet());
		} catch (Exception e) {
			System.out.println("查询用户地址列表失败");
		}
		return address;
	}

	@Override
	public Customer queryCustomer(String customerId) {
		Customer customer = new Customer();
		try {
			customer=em.find(Customer.class, customerId);
		} catch (Exception e) {
			System.out.println("用户信息查询错误");
		}
		return customer;
	}

}
